// pages/luck_draw/luck_draw.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chou:false,
    zhe:false,
    jine:''
  },
  hide_model:function(){
    var that = this;
    that.setData({
      chou: false,
      zhe: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  chou:function(){
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    var that = this;
    if(that.data.huiyuanxinxi.lottery<=0){
      wx.showToast({
        title: '您没有抽奖机会了',
        icon:"none"
      })
      return false
    }
    // 获取随机金额
    wx.request({
      url: app.globalData.url+'jingcai.php?Action=choujiangjiner',
      success:function(res){
        console.log(res)
        wx.hideLoading()
        that.setData({
          chou: true,
          zhe: true,
          jine: res.data
        })
        // 抽奖
      
        wx.request({
          url: app.globalData.url +'jingcai.php?Action=choujiangchaozuo',
          data:{
            openid:app.globalData.opid,
            money:res.data
          },
          success:function(res){
            console.log('抽奖')
            console.log(res)
            // 抽奖次数+累计奖金 再次刷新
           
            wx.request({
              url: app.globalData.url + 'jingcai.php?Action=huiyuanxinxi',
              data: {
                openid: app.globalData.opid
              },
              success: function (res) {
                console.log('再次刷新')
                console.log(res)
                that.setData({
                  huiyuanxinxi: res.data
                })

              }
            })
          }
        })
        
      }
    })
    
  },
  go_rule:function(e){
    wx.navigateTo({
      url: '../rule1/rule1',
    })
  },
  go_url:function(e){
    wx.navigateTo({
      url: '../choujiang_list/choujiang_list',
    })
  },
  shengcheng:function(res){
    var that = this;
    wx.showLoading({
      title: '海报生成中...',
    })
    wx.request({
      url: app.globalData.url+'ziyu.php?Action=scerweima',
      data:{
        openid:app.globalData.opid
      },
      success:function(res){
        console.log(res)
        var width = that.data.canvasWidth;
        var rem = width / 750;
        var arr = [];
        wx.downloadFile({
          url: res.data.erweima,
          success: function (res) {
            console.log(res.tempFilePath);
            
            // const wxGetImageInfo = promisify(wx.getImageInfo)
            var context = wx.createCanvasContext('myCanvas');
            var path = "./img/bg.png";
            //将模板图片绘制到canvas,在开发工具中drawImage()函数有问题，不显示图片
            //不知道是什么原因，手机环境能正常显示
            context.drawImage(path, 0, 0, 700*rem, 1250*rem);
            var path1 = res.tempFilePath;
            context.drawImage(path1, 40 * rem, 850 * rem, 240 * rem, 240 * rem);


            context.draw();
            setTimeout(function () {
              wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: 700 * rem,
                height: 1250 * rem,
                destWidth: 700 * rem*2,
                destHeight: 1250 * rem*2,
                canvasId: 'myCanvas',
                success: function (res) {
                  console.log(res.tempFilePath)

                  var arr = [];
                  arr.push(res.tempFilePath)
                  wx.previewImage({
                    current: "", // 当前显示图片的http链接
                    urls: arr // 需要预览的图片http链接列表
                  });
                  wx.hideLoading()
                  that.setData({
                    zhe: false,
                    fenxiang: false
                  })
                  wx.request({
                    url: app.globalData.url + 'voting.php?Action=ffshare',
                    data: {
                      openid: app.globalData.opid,
                      S_types: 3
                    },
                    success: function (res) {
                      console.log(res)
                    }
                  })
                },
                fail: function (res) {
                  console.log(res)
                }
              })
            }, 1000);
            that.setData({
              zhe: true,
              fenxiang: true
            })

          }, fail: function (fres) {

          }
        })
        
       
       
      }
      

    })
 
   
   
   
  } ,
  hide:function(){
    this.setData({
      zhe: false,
      fenxiang: false
    })
  },
  onLoad: function (options) {
    var that = this;
    // 抽奖次数+累计奖金
    if(options.id){
      that.setData({
        lottery: options.id
      })
    }
    wx.request({
      url: app.globalData.url + 'jingcai.php?Action=huiyuanxinxi',
      data: {
        openid: app.globalData.opid
      },
      success: function (res) {
        console.log(res)
        that.setData({
          huiyuanxinxi: res.data
        })

      }
    })
    var myCanvasWidth;
    var myCanvasHeight;

    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowWidth)
        console.log(res.windowHeight)
        myCanvasWidth = res.windowWidth;
        myCanvasHeight = res.windowHeight;
      },
    })
    that.setData({
      canvasWidth: myCanvasWidth,
      canvasHeight: myCanvasHeight
    })
  
   
   
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    // 抽奖次数+累计奖金
      wx.request({
        url: app.globalData.url + 'jingcai.php?Action=huiyuanxinxi',
        data: {
          openid: app.globalData.opid
        },
        success: function (res) {
          console.log(res)
          that.setData({
            huiyuanxinxi: res.data
          })
          console.log(res.data)
          wx.hideLoading()

        }
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})